package com.spic.business.plan.comunication.controller;

import com.spic.business.common.core.utils.poi.BusinessExcelUtil;
import com.spic.business.plan.comunication.domain.PlanCommunicationSupport;
import com.spic.business.plan.comunication.service.IPlanCommunicationSupportService;
import com.spic.common.core.utils.SecurityUtils;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 应急通讯保障Controller
 * 
 * @author spic
 * @date 2021-10-15
 */
@RestController
@RequestMapping("plan/comunication")
public class PlanCommunicationSupportController extends BaseController
{
    @Autowired
    private IPlanCommunicationSupportService planCommunicationSupportService;

    /**
     * 查询应急通讯保障列表
     */
    @PreAuthorize(hasPermi = "business/plan:comunication:list")
    @GetMapping("/list")
    public TableDataInfo list(PlanCommunicationSupport planCommunicationSupport)
    {
        startPage();
        List<PlanCommunicationSupport> list = planCommunicationSupportService.selectPlanCommunicationSupportList(planCommunicationSupport);
        return getDataTable(list);
    }

    /**
     * 导出应急通讯保障列表
     */
    @PreAuthorize(hasPermi = "business/plan:comunication:export")
    @Log(title = "应急通讯保障", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlanCommunicationSupport planCommunicationSupport) throws IOException
    {
        List<PlanCommunicationSupport> list = planCommunicationSupportService.selectPlanCommunicationSupportList(planCommunicationSupport);
        ExcelUtil<PlanCommunicationSupport> util = new BusinessExcelUtil<>(PlanCommunicationSupport.class);
        util.exportExcel(response, list, "应急通讯保障数据");
    }

    /**
     * 获取应急通讯保障详细信息
     */
    @PreAuthorize(hasPermi = "business/plan:comunication:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(planCommunicationSupportService.selectPlanCommunicationSupportById(id));
    }

    /**
     * 新增应急通讯保障
     */
    @PreAuthorize(hasPermi = "business/plan:comunication:add")
    @Log(title = "应急通讯保障", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlanCommunicationSupport planCommunicationSupport)
    {
        return toAjax(planCommunicationSupportService.insertPlanCommunicationSupport(planCommunicationSupport));
    }

    /**
     * 修改应急通讯保障
     */
    @PreAuthorize(hasPermi = "business/plan:comunication:edit")
    @Log(title = "应急通讯保障", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlanCommunicationSupport planCommunicationSupport)
    {
        return toAjax(planCommunicationSupportService.updatePlanCommunicationSupport(planCommunicationSupport));
    }

    /**
     * 删除应急通讯保障
     */
    @PreAuthorize(hasPermi = "business/plan:comunication:remove")
    @Log(title = "应急通讯保障", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(planCommunicationSupportService.deletePlanCommunicationSupportByIds(ids));
    }

    /**
     * 导入应急通讯记录
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PreAuthorize(hasPermi = "business/plan:comunication:import")
    @Log(title = "应急通讯保障", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<PlanCommunicationSupport> util = new BusinessExcelUtil<PlanCommunicationSupport>(PlanCommunicationSupport.class);
        List<PlanCommunicationSupport> PlanCommunicationSupportList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = planCommunicationSupportService.importData(PlanCommunicationSupportList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 下载导入模板
     * @return
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<PlanCommunicationSupport> util = new ExcelUtil<PlanCommunicationSupport>(PlanCommunicationSupport.class);
        util.importTemplateExcel(response, "用户数据");
    }
}
